package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.manager.mapper.SysMenuMapper;
import com.atguigu.spzx.manager.mapper.SysRoleMenuMapper;
import com.atguigu.spzx.manager.service.SysMenuService;
import com.atguigu.spzx.manager.service.SysRoleMenuService;
import com.atguigu.spzx.model.dto.system.AssginMenuDto;
import com.atguigu.spzx.model.entity.system.SysMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SysRoleMenuServiceImpl implements SysRoleMenuService {
    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;
    @Autowired
    private SysMenuService sysMenuService;
    @Override
    public Map<String, Object> findSysRoleMenuByRoleId(Long roleId) {
        Map<String,Object> map=new HashMap<>();
        //查找所有菜单转换成树形结构
        List<SysMenu> listFamily=sysMenuService.findNodes();
        //查询当前RoleId对应的菜单
        List<Long> roleMenuIds =sysRoleMenuMapper.selectMenusByRoleId(roleId);
        map.put("sysMenuList",listFamily);
        map.put("roleMenuIds",roleMenuIds);
        return map;
    }

    @Override
    public void doAssign(AssginMenuDto assginMenuDto) {
        //先删除之前的在添加新的
        sysRoleMenuMapper.deleteByRoleId(assginMenuDto.getRoleId());
        sysRoleMenuMapper.insert(assginMenuDto);
    }
}
